$(function () {
    /*     var loading = weui.loading('loading', {
            className: 'custom-classname'
        }); */
    location.href = "#bottom";
    var cid = Common.fn.getUrlParam('cid'),
        nickname = Common.fn.getUrlParam('nickname');
    if (!cid) {
        weui.alert('用户参数错误');
        location.href = 'message.html';
    }
    var result = Model.message.messageDetail({
        c_openid: cid
    });
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    // 信息
    $('.nickname').text(result.data[0].business.nickanme);
    if (!result.data[0].business.nickanme) {
        $('.nickname').text(nickname);
    }
    /*
     左侧 接收别人信息 调用，三个参数：leftBubble，头像路径，内容
     chat(element,imgSrc,doctextContent)
     */
    for (var i = 0; i < result.data[0].message.length; i++) {
        var messages = result.data[0].message;
        // 如果是对方发送信息
        if (messages[i].m_openid == cid) {
            chat("leftBubble", result.data[0].business.photo, messages[i].content);
        } else {
            // 自己发送的信息
            chat("rightBubble", result.data[0].crrentuser, messages[i].content, 1);
        }
    }


    $(".send-btn").click(function () {
        //清空输入框
        var result = Model.message.putMessage({
            s_openid: cid,
            content: $.trim($('.chat-info').html())
        });
        if (result.code) {
            Common.fn.formatOutput(result);
            return false;
        }
        location.href = "#bottom";
        location.reload();
        $('.chat-info').html('');
    });
    loading.hide();
});

function chat(element, imgSrc, doctextContent, type) {
    //判断传入的值，是左侧还是右侧
    var $user = element;
    var $doctorHead = imgSrc;
    //获取输入的值
    if (type == 1) {
        var $textContent = doctextContent;
    } else {
        var $textContent = $('.chat-info').html();
    }
    //获取传入的卖家输入的内容
    var $doctextContent = doctextContent;
    //获取容器
    var $box = $('.bubbleDiv');
    var $boxHeght = $box.height();
    var $sectionHeght = $(".chat-box").height();
    var $elvHeght = Math.abs($boxHeght - $sectionHeght);
    //卖家
    if ($user === "leftBubble") {
        $box.append(createdoct(imgSrc, $doctextContent)).animate({
            scrollTop: $(document).height()
        }, 300);
    }
    //买家
    else if ($user === "rightBubble") {
        $box.append(createuser($textContent, imgSrc)).animate({
            scrollTop: $(document).height()
        }, 300);
    } else {
        console.log("请传入必须的参数");
    }

}

function createdoct(imgSrc, $doctextContent) {
    var $textContent = $doctextContent;
    var $imgSrc = imgSrc;
    var block;
    if ($textContent == '' || $textContent == 'null') {
        alert('亲！别太着急，先说点什么～');
        return;
    }
    block = '<div class="bubbleItem">' +
        '<div class="doctor-head">' +
        '<img src="' + imgSrc + '" alt="doctor"/>' +
        '</div>' +
        '<span class="bubble leftBubble">' + $textContent + '<span class="topLevel"></span></span>' +
        '</div>';

    return block;
};

function createuser($textContent, imgSrc) {
    var $textContent = $textContent;
    var $imgSrc = imgSrc;
    var block;
    if ($textContent == '' || $textContent == 'null') {
        alert('亲！别太着急，先说点什么～');
        return;
    }

    block = '<div class="bubbleItem clearfix">' +
        '<span class="bubble rightBubble">' + $textContent +
        '<span class="topLevel"></span>' +
        '<img src="' + imgSrc + '" alt="user"/>' +
        '</span></div>';
    return block;
};

// 限制输入文字字数
$(document).on('keydown', '.chat-info', function () {
    var length = $(this).text().length;
    if (length >= 200) {
        $(this).text($(this).text().substr(0, 199));
    }
});